package com.fx.spring.config;

import com.fx.spring.annotation.ControllerFX;
import com.fx.spring.controller.AbstractController;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 自动加载FXML
 */
public class AutoLoaderFxml implements BeanPostProcessor {

    // 在Spring Bean实例化完成后自动加载FXML
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> beanClass = bean.getClass();

        if (beanClass.isAnnotationPresent(ControllerFX.class)) {
            ControllerFX annotation = beanClass.getAnnotation(ControllerFX.class);
            if (bean instanceof AbstractController) {
                ((AbstractController<?>) bean).setFxmlLoader(annotation.fxmlURL());
            }
        }
        return bean;
    }
}
